package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.AccountException;
import com.management.commons.exception.ExceptionCode.AccountExceptionCode;
import com.management.controller.form.*;
import com.management.controller.info.AccountPageInfo;
import com.management.controller.info.GetMyProjectInfo;
import com.management.controller.info.MemberPageInfo;
import com.management.controller.info.ProjectAccountPageInfo;
import com.management.model.Account;
import com.management.model.AccountRoleProject;
import com.management.dao.AccountRoleProjectDao;
import com.management.model.ProjectInfo;
import com.management.model.Role;
import com.management.service.AccountRoleProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.service.AccountService;
import com.management.service.ProjectInfoService;
import com.management.service.RoleService;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-24
 */
@Service
public class AccountRoleProjectServiceImpl extends ServiceImpl<AccountRoleProjectDao, AccountRoleProject> implements AccountRoleProjectService {
    @Resource
    private AccountService accountService;
    @Resource
    private RoleService roleService;
    @Resource
    private ProjectInfoService projectInfoService;
    @Override
    public Result addProjectRole(AddProjectRoleForm addProjectRoleForm) {
        QueryWrapper<AccountRoleProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",addProjectRoleForm.getProjectId());
        queryWrapper.eq("role_num",addProjectRoleForm.getRoleNum());
        queryWrapper.eq("account_id",addProjectRoleForm.getAccountId());
        AccountRoleProject one = getOne(queryWrapper);
        if(one!=null){
            MemberPageInfo memberPageInfo = new MemberPageInfo();
            Account byId = accountService.getById(addProjectRoleForm.getAccountId());
            Role byId1 = roleService.getById(addProjectRoleForm.getRoleNum());
            ProjectInfo byId2 = projectInfoService.getById(addProjectRoleForm.getProjectId());
            memberPageInfo.setRoleName(byId1.getRoleName());
            memberPageInfo.setProjectName(byId2.getProjectName());
            memberPageInfo.setAccountName(byId.getAccountName());
            return Result.getInstance(MemberPageInfo.class).setData(memberPageInfo).setCode(5004);
        }
        AccountRoleProject accountRoleProject = CopyUtils.CopyOne(addProjectRoleForm, AccountRoleProject.class);
        boolean save = save(accountRoleProject);
        if(save){
            return Result.getInstance().setCode(200).setMessage("分配成功");
        }
        throw new AccountException(AccountExceptionCode.ADDPROJECTROLE_ERROR);
    }

    @Override
    public Result getAllMember(MemberPageForm memberPageForm) {
        MemberPageForm page = page(memberPageForm);
        List<AccountRoleProject> records = page.getRecords();
        List<MemberPageInfo> result=new ArrayList<>();
        for (AccountRoleProject record : records) {
            Account byId = accountService.getById(record.getAccountId());
            Role byId1 = roleService.getById(record.getRoleNum());
            ProjectInfo byId2 = projectInfoService.getById(record.getProjectId());
            MemberPageInfo info = new MemberPageInfo();
            info.setAccountName(byId.getAccountName());
            info.setAccountRoleProjectId(record.getAccountRoleProjectId());
            info.setProjectName(byId2.getProjectName());
            info.setRoleName(byId1.getRoleName());
            result.add(info);
        }
        Page<MemberPageInfo> infoPage=new Page<>();
        infoPage.setRecords(result);
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setPages(page.getPages());
        infoPage.setCurrent(page.getCurrent());
        return Result.getInstance(Page.class).setData(infoPage);
    }

    @Override
    public Result findAccountByProject(ProjectAccountForm projectAccountForm) {
        QueryWrapper<AccountRoleProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectAccountForm.getProjectId());
        ProjectAccountForm page = page(projectAccountForm, queryWrapper);
        List<AccountRoleProject> records = page.getRecords();
        List<ProjectAccountPageInfo> result=new ArrayList<>();
        for (AccountRoleProject record : records) {
            Account byId = accountService.getById(record.getAccountId());
            Role byId1 = roleService.getById(record.getRoleNum());
            ProjectAccountPageInfo projectAccountPageInfo = new ProjectAccountPageInfo();
            projectAccountPageInfo.setAccountName(byId.getAccountName());
            projectAccountPageInfo.setRoleName(byId1.getRoleName());
            result.add(projectAccountPageInfo);
        }
        Page<ProjectAccountPageInfo> page1=new Page<>();
        page1.setRecords(result);
        page1.setCurrent(page.getCurrent());
        page1.setPages(page.getPages());
        page1.setTotal(page.getTotal());
        page1.setSize(page.getSize());
        return Result.getInstance(Page.class).setData(page1);
    }

    @Override
    public Result delAccount(DelAccountForm delAccountForm) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",delAccountForm.getAccountName());
        Account one = accountService.getOne(queryWrapper);
        QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_name",delAccountForm.getRoleName());

        Role one1 = roleService.getOne(queryWrapper1);
        QueryWrapper<ProjectInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("project_name",delAccountForm.getProjectName());
        ProjectInfo one2 = projectInfoService.getOne(queryWrapper2);
        UpdateWrapper<AccountRoleProject> queryWrapper3 = new UpdateWrapper<>();
        queryWrapper3.eq("account_id",one.getAccountId());
        queryWrapper3.eq("project_id",one2.getProjectId());
        queryWrapper3.eq("role_num",one1.getRoleNum());
        boolean remove = remove(queryWrapper3);
        if(remove){
            return Result.getInstance().setCode(200).setMessage("操作成功");
        }
        throw new AccountException(AccountExceptionCode.REMOVEACCOUNTROLEPROJECT_ERROR);
    }

    @Override
    public Result getMyProject(GetMyProjectForm getMyProjectForm) {
        QueryWrapper<AccountRoleProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",getMyProjectForm.getAccountId());
        GetMyProjectForm page = page(getMyProjectForm, queryWrapper);
        List<AccountRoleProject> records = page.getRecords();
        List<GetMyProjectInfo> list=new ArrayList<>();
        for (AccountRoleProject record : records) {
            GetMyProjectInfo getMyProjectInfo = new GetMyProjectInfo();
            Role byId = roleService.getById(record.getRoleNum());
            ProjectInfo byId1 = projectInfoService.getById(record.getProjectId());
            getMyProjectInfo.setProjectName(byId1.getProjectName());
            getMyProjectInfo.setRoleName(byId.getRoleName());
            list.add(getMyProjectInfo);
        }
        Page<GetMyProjectInfo> infoPage=new Page<>();
        infoPage.setRecords(list);
        infoPage.setTotal(page.getTotal());
        infoPage.setSize(page.getSize());
        infoPage.setCurrent(page.getCurrent());
        infoPage.setPages(page.getPages());
        return Result.getInstance(Page.class).setData(infoPage);
    }
}
